package com.CityHunter.DataObject;

import java.util.ArrayList;
import java.util.Date;

import android.util.Log;

import com.CityHunter.Factory.SchedulePointFactory;
import com.google.android.maps.GeoPoint;

public class Schedule {

	private int scheduleId;
	private ArrayList<SchedulePoint> schedulePoints;
	private Date startTime,endTime;
	private String description;
	private SchedulePointFactory factory;

	public Schedule() {
		schedulePoints = new ArrayList<SchedulePoint>();
		factory = new SchedulePointFactory();
	}
	
	public Schedule(Date startTime,Date endTime,String description) {
		schedulePoints = new ArrayList<SchedulePoint>();
		factory = new SchedulePointFactory();
		setStartTime(startTime);
		setEndTime(endTime);
		setDescription(description);
	}

	
	
	public int getScheduleId() {
		return scheduleId;
	}

	public void setScheduleId(int scheduleId) {
		this.scheduleId = scheduleId;
	}

	public void addPoint(GeoPoint geoPoint) {
		schedulePoints.add(factory.createSchedulePoint(geoPoint));
	}

	public void addPoint(GeoPoint geoPoint, Date date, String description) {
		schedulePoints.add(factory.createSchedulePoint(geoPoint, date,
				description));
		sort();
	}

	public SchedulePoint remove(int index) {
		return schedulePoints.remove(index);
	}

	public SchedulePoint get(int index) {
		return schedulePoints.get(index);
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public int size() {
		return schedulePoints.size();
	}

	public String toString() {
		String output = "Schedule Points: ";
		for (int i = 0; i < schedulePoints.size(); i++) {
			output += "\n" + schedulePoints.toString();
		}
		return output;
	}

	public void sort() {
		if (schedulePoints.size() > 1) {
			ArrayList<SchedulePoint> temp = schedulePoints;
			int size = temp.size();
			schedulePoints = new ArrayList<SchedulePoint>();
			for (int x = 0; x < size; x++) {
				SchedulePoint firstPoint = temp.get(0);
				Log.d("Debug", firstPoint.toString());
				for (int y = temp.size() - 1; y > 0; y--) {
					SchedulePoint currentPoint = temp.get(y);
					if (currentPoint.getTime().compareTo(firstPoint.getTime()) < 0) {
						firstPoint = currentPoint;
					}
				}
				temp.remove(firstPoint);
				schedulePoints.add(firstPoint);
			}
		}
		indexing();
	}

	public void indexing() {
		for (int i = 0; i < schedulePoints.size(); i++) {
			schedulePoints.get(i).setOrder(i);
		}
	}
}
